//
//  AllNodesInScrollView.m
//  NaRiLSVMT
//
//  Created by jmsre on 14-8-20.
//  Copyright (c) 2014年 helloyouth.org. All rights reserved.
//

#import "AllNodesInScrollView.h"
#import "SVGloble.h"
#import "TopLargerScreenView.h"
#import "FlowDetailViewController.h"
#import "MBProgressController.h"


#define kMaxDragLength  (700)
#define kMaxToggleLength        (250)

//按钮空隙
#define BUTTONGAP 5
//滑条宽度
#define CONTENTSIZEX (Screen_Width-20)
//按钮id
#define BUTTONID (sender.tag-100)
//滑动id
#define BUTTONSELECTEDID (scrollViewSelectedChannelID - 100)



//extern  BOOL    isNodePagePlay;//是否是在节点界面播放。是：YES.否：NO.
extern  NSInteger   lastFlowNumber;//前一个处理的流程No.或者当前

//UIImageView *shadowImageView = nil;   //投屏或者播放黄色边框


dispatch_source_t timer_playFlowScreen;//播放定时器
BOOL    isPlayingFlag = NO;//是否正在单次播放或者循环播放标识
static  NSInteger   onceOrcycles = 0;//循环播放还是单次播放

BOOL    isProjectScreen = NO;//是否拖屏了

CGPoint lastPoint;//节点拖动的上一个点坐标

extern BOOL    isNotNeedTurnNode;//如果是本流程在播放，则需要切节点，否则不需要切节点

extern  NSInteger   isCanActionOtherButton;//禁止快速点击播放按钮

extern  BOOL    isHaveFlowPlaying;//是否有流程正在播放
extern  BOOL    currentFlowIsPlaying;//是否是当前流程在播放

@interface AllNodesInScrollView()
{
    
}
@end

@implementation AllNodesInScrollView
@synthesize nameArray;
@synthesize scrollViewSelectedChannelID;

//+ (AllNodesInScrollView *)shareInstance {
//    static AllNodesInScrollView *_instance;
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        _instance=[[self alloc] initWithFrame:CGRectMake(0, 0, CONTENTSIZEX, 400)];
//    });
//    return _instance;
//}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.delegate = self;
        self.backgroundColor = [UIColor clearColor];
        self.pagingEnabled = NO;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        
        userSelectedChannelID = 100;
        scrollViewSelectedChannelID = 100;
        
        self.buttonOriginXArray = [NSMutableArray array];
        self.buttonWithArray = [NSMutableArray array];
    }
    return self;
}


- (void)initWithNameButtons
{
    /*****************重新初始化一些变量*******************/
    userSelectedChannelID = 100;
    scrollViewSelectedChannelID = 100;
    [self.buttonOriginXArray removeAllObjects];
    [self.buttonWithArray removeAllObjects];
    self.buttonOriginXArray = nil;
    self.buttonWithArray = nil;
    self.buttonOriginXArray = [NSMutableArray array];
    self.buttonWithArray = [NSMutableArray array];
    [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"nodeButtonArr"] enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL *stop){
        NSDictionary *dic = (NSDictionary *)obj;
        UIButton *button = [dic objectForKey:@(idx)];
        button.selected = NO;
        button = nil;
    }];
    [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"nodeButtonArr"] removeAllObjects];
//    buttonArray = nil;
    self.pannedView = nil;
//    self.pagingEnabled = NO;
//    self.panGestureRecognizer.enabled = NO;
    
    /***************************************************/
    float xPos = 5.0;
    int buttonRow = 0;//按钮的行数
    BOOL    flag = NO;//是否折行标志
    for (int i = 0; i < [self.nameArray count]; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        NSString *title = [self.nameArray objectAtIndex:i];
        
        [button setTag:i+100];
        if (i == 0) {
            button.selected = YES;
        }
        [button setTitle:title forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:20.0];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(selectNameButton:) forControlEvents:UIControlEventTouchUpInside];
        
        /**********************存放节点按钮**********************/

            [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"nodeButtonArr"] addObject:@{@(i): button}];
//        }
        /********************************************/
        
        //长按节点:
        UILongPressGestureRecognizer    *buttonLongPan = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPanGesture:)];
        buttonLongPan.delegate = self;
        buttonLongPan.minimumPressDuration = 0.5;
        [button addGestureRecognizer:buttonLongPan];
        
        
        //拖动投放:

        /*************************************************************/

        if(flag == NO)
        {
            button.frame = CGRectMake(xPos, 10+buttonRow*(10+90), 185, 90);
        }
        else
        {
            flag = NO;
            button.frame = CGRectMake(xPos, 10+buttonRow*(10+90), 185, 90);
        }
        //[button setBackgroundColor:[UIColor grayColor]];
        if(buttonRow%2 == 0)
        {
            if(xPos > 190*4+5+5)
            {
                buttonRow++;
                flag = YES;
                xPos -= 185+BUTTONGAP;
                button.frame = CGRectMake(xPos, 10+buttonRow*(10+90), 185, 90);
            }

            [_buttonOriginXArray addObject:@{@"xPos":@(xPos),@"yPos":@(10+buttonRow*(10+90))}];
            
            if(flag == NO)
            {
                xPos += 185+BUTTONGAP;
            }
            else
            {
                xPos -= 185+BUTTONGAP;
            }
            
            [_buttonWithArray addObject:@(button.frame.size.width)];
            
            [self addSubview:button];
            
            if(i==0)
            {
                [button setBackgroundImage:[UIImage imageNamed:@"firstRow_1.png"] forState:UIControlStateNormal];
                [button setBackgroundImage:[UIImage imageNamed:@"firstRow_1_3.png"] forState:UIControlStateSelected];
            }
            else
            {
                switch (i%5) {
                    case 0:
                    {
                        [button setBackgroundImage:[UIImage imageNamed:@"twoRow_11.png"] forState:UIControlStateNormal];
                        [button setBackgroundImage:[UIImage imageNamed:@"twoRow_11_3.png"] forState:UIControlStateSelected];
                    }
                        break;
                    case 1:
                    case 2:
                    case 3:
                    {
                        [button setBackgroundImage:[UIImage imageNamed:@"firstRow_234.png"] forState:UIControlStateNormal];
                        [button setBackgroundImage:[UIImage imageNamed:@"firstRow_234_3.png"] forState:UIControlStateSelected];
                    }
                        break;
                    case 4:
                    {
                        [button setBackgroundImage:[UIImage imageNamed:@"firstRow_5.png"] forState:UIControlStateNormal];
                        [button setBackgroundImage:[UIImage imageNamed:@"firstRow_5_3.png"] forState:UIControlStateSelected];
                    }
                        break;
                    default:
                        break;
                }
            }
        }
        else
        {
            if(xPos < 0)
            {
                buttonRow++;
                flag = YES;
                xPos += 185+BUTTONGAP;
                button.frame = CGRectMake(xPos, 10+buttonRow*(10+90), 185, 90);
            }
            [_buttonOriginXArray addObject:@{@"xPos":@(xPos),@"yPos":@(10+buttonRow*(10+90))}];
            
            if(flag == NO)
            {
                xPos -= 185+BUTTONGAP;
            }
            else
            {
                xPos +=185+BUTTONGAP;
            }
            
            [_buttonWithArray addObject:@(button.frame.size.width)];
            
            
            [self addSubview:button];
            
            switch (i%5) {
                case 0:
                {
                    [button setBackgroundImage:[UIImage imageNamed:@"threeRow_10.png"] forState:UIControlStateNormal];
                    [button setBackgroundImage:[UIImage imageNamed:@"threeRow_10_3.png"] forState:UIControlStateSelected];
                }
                    break;
                case 1:
                case 2:
                case 3:
                {
                    [button setBackgroundImage:[UIImage imageNamed:@"twoRow_987.png"] forState:UIControlStateNormal];
                    [button setBackgroundImage:[UIImage imageNamed:@"twoRow_987_3.png"] forState:UIControlStateSelected];
                }
                    break;
                case 4:
                {
                    [button setBackgroundImage:[UIImage imageNamed:@"twoRow_6.png"] forState:UIControlStateNormal];
                    [button setBackgroundImage:[UIImage imageNamed:@"twoRow_6_3.png"] forState:UIControlStateSelected];
                }
                    break;
                default:
                    break;
            }
        }
    }
    
    self.contentSize = CGSizeMake(944, (buttonRow+1)*100);
    self.dHeight = 0;
    
}

//封装一个函数用于：循环或者单次播放:
- (void)onceOrcyclesPlay:(NSMutableArray *)buttonArr StartIndex:(NSInteger)index Interval:(NSInteger)time IsCyclesPlay: (BOOL)flag isPause:(NSInteger)pause
{
    if(flag == NO)
    {
        onceOrcycles = 1;//单次:为了点其他按钮暂停播放
    }
    else
    {
        onceOrcycles = 2;//循环:为了点其他按钮暂停播放
    }
    
    if([allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"isNodePagePlay"] intValue] == 0){
        isCanActionOtherButton = 0;
    }
    //停止播放
    if(pause == 1)
    {
        if (timer_playFlowScreen != nil) {
        dispatch_source_cancel(timer_playFlowScreen);
        }
        allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"playFlag"] = @(0);
        allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"pauseFlag"] = @(1);
        return;
    }
    
    __block NSInteger   indexButton = index;
    
    __block NSInteger   endIndexButton = 0;
    if (indexButton == 0)
    {
        endIndexButton = [buttonArr count]-1;
    }
    else
    {
        endIndexButton = indexButton-1;
    }
    
    __block AllNodesInScrollView *allNodesView = allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"bottomSmaPic"];
    
    timer_playFlowScreen = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
    dispatch_source_set_timer(timer_playFlowScreen, DISPATCH_TIME_NOW, time * NSEC_PER_SEC, 1ull * NSEC_PER_SEC);
    dispatch_source_set_event_handler(timer_playFlowScreen, ^{
        NSLog(@"screen play!");
        
        isProjectScreen = NO;

        
        NSDictionary *dic = (NSDictionary *)[buttonArr objectAtIndex:indexButton];
        UIButton *button = [dic objectForKey:@(indexButton)];
        userSelectedChannelID = button.tag;
        
        //切节点，不需要切屏幕
        if([allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"playFlag"] integerValue] == 1 && isNotNeedTurnNode == YES)
        {
            [allNodesView selectNameButton:button];
        }
        
        [allNodesView setContentOffset:CGPointMake(self.frame.origin.x, button.frame.origin.y-10) animated:YES];
        
        CGRect f = CGRectMake(0, 0, 1004, 390);
        //是否在节点页面播放的
        if ([allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"isNodePagePlay"] intValue] == 1)
        {
            [allNodesView.delagateNodeForTapOrPlay HandleNoteButtonLongPressPlay:self ButtonID:indexButton RECT:f];
        }
        else
        {
            [allNodesView.delagateFlowForTapOrPlay HandleNoteButtonLongPressPlay:self ButtonID:indexButton RECT:f];
        }
        
        
        if(indexButton == endIndexButton)
        {
            if(flag == NO)
            {
                if([allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"isNodePagePlay"] intValue] == 1){
                    if (timer_playFlowScreen != nil) {
                dispatch_source_cancel(timer_playFlowScreen);
                    }
                    //结束单次播放
                [allNodesView.delagateNodeForTapOrPlay HandlePausePlay:allNodesView ButtonIndex:@(onceOrcycles)];
                }
                else
                {
                    if (timer_playFlowScreen != nil) {
                        dispatch_source_cancel(timer_playFlowScreen);
                    }
                    //结束单次播放
                    [allNodesView.delagateFlowForTapOrPlay HandleFlowControlPausePlay:allNodesView ButtonIndex:@(onceOrcycles)];
                }
            }
            else
            {
                if(indexButton == [buttonArr count]-1)
                {
                    indexButton = 0;
                }
                else
                {
                    indexButton++;
                }
            }
        }
        else
        {
            if(indexButton == [buttonArr count]-1)
            {
                indexButton = 0;
            }
            else
            {
                indexButton++;
            }
        }
        allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"havePlayNodeIndex"] = @(indexButton);
    });
    
    dispatch_resume(timer_playFlowScreen);
}

//点击节点
- (void)selectNameButton:(UIButton *)sender
{
    [self adjustScrollViewContentX:sender];
    
    if([allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"isNodePagePlay"] intValue] == 1){
    if([allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"playFlag"] intValue] == 1)
    {
        if(sender.tag-100 != userSelectedChannelID-100 /*&& sender.tag-100 !=userSelectedChannelID-100 +1*/)//点其他按钮暂停
        {
            AllNodesInScrollView *allNodesView = allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"bottomSmaPic"];
            if (timer_playFlowScreen != nil) {
            dispatch_source_cancel(timer_playFlowScreen);
            }
            allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"playFlag"] = @(0);
            allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"pauseFlag"] = @(1);
            //可以发一个通知，让按钮再发一个函数
            [allNodesView.delagateNodeForTapOrPlay HandlePausePlay:self ButtonIndex:@(onceOrcycles)];
        }
    }
    }
    
    if([allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"isNodePagePlay"] intValue] == 1){
    //如果更换按钮
        [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"nodeButtonArr"] enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL *stop){
            NSDictionary *dic = (NSDictionary *)obj;
            UIButton *button = [dic objectForKey:@(idx)];
            button.selected = NO;
        }];
        if (sender.tag != StringFromId(allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"haveChooseNodeIndex"]).integerValue+100) {
            //取之前的按钮
            UIButton *lastButton = [[allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"nodeButtonArr"] objectAtIndex:StringFromId(allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"haveChooseNodeIndex"]).integerValue]objectForKey:allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"haveChooseNodeIndex"]];
            lastButton.selected = NO;
            //赋值按钮ID
            userSelectedChannelID = sender.tag;
            allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"haveChooseNodeIndex"] = @(userSelectedChannelID-100);
        }
    }
    else
    {
        [allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"nodeButtonArr"] enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL *stop){
            NSDictionary *dic = (NSDictionary *)obj;
            UIButton *button = [dic objectForKey:@(idx)];
            button.selected = NO;
        }];
        if (sender.tag != StringFromId(allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"haveChooseNodeIndex"]).integerValue+100) {
            //取之前的按钮
            UIButton *lastButton = [[allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"nodeButtonArr"] objectAtIndex:StringFromId(allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"haveChooseNodeIndex"]).integerValue]objectForKey:allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"haveChooseNodeIndex"]];
            lastButton.selected = NO;
            //赋值按钮ID
            userSelectedChannelID = sender.tag;
            allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"haveChooseNodeIndex"] = @(userSelectedChannelID-100);
        }
    }
    
    if([allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"isNodePagePlay"] intValue] == 1){
    if([allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"playFlag"] intValue] == 0)
    {
    [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"nodeButtonArr"] enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL *stop){
        NSDictionary *dic = (NSDictionary *)obj;
        UIButton *button = [dic objectForKey:@(idx)];
        [button addTarget:self action:@selector(selectNameButton:) forControlEvents:UIControlEventTouchUpInside];
        for (UIGestureRecognizer *v in button.gestureRecognizers) {
            if ([v isKindOfClass:[UIPanGestureRecognizer class]]) {
                [button removeGestureRecognizer:v];
            }
        }
        [button.layer removeAllAnimations];
    }];
    }
    }
    
    //按钮选中状态
    if (!sender.selected) {
        if([allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"isNodePagePlay"] intValue] == 1){
        if([allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"playFlag"] intValue] == 1)
        {
            sender.selected = NO;
        }
        else
        {
            sender.selected = YES;
        }
        }
        WeakSelf;
        [UIView animateWithDuration:0.25 animations:^{
            
            float originX = [[[_buttonOriginXArray objectAtIndex:BUTTONID] objectForKey:@"xPos"] floatValue];
            float originY = [[[_buttonOriginXArray objectAtIndex:BUTTONID] objectForKey:@"yPos"] floatValue];
            if([allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"isNodePagePlay"] intValue] == 1){
            if([allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"playFlag"] intValue] == 1)
            {
                NSDictionary *dic = (NSDictionary *)[allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"nodeButtonArr"] objectAtIndex:BUTTONID];
                UIButton *button = [dic objectForKey:@(BUTTONID)];
                [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"shadowImageView"] setFrame:CGRectMake(originX, originY, button.frame.size.width, button.frame.size.height)];
                /************************************************************************/
                [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"nodeButtonArr"] enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL *stop){
                    NSLog(@"%lu",(unsigned long)idx);
                    if([allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"havePlayNodeIndex"] integerValue] == idx)
                    {
                        if((idx/5)%2 == 0)
                        {
                            if(idx==0)
                            {
                                [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"shadowImageView"] setImage:[UIImage imageNamed:@"firstRow_1_2.png"]];
                            }
                            else
                            {
                                switch ((idx)%5) {
                                    case 0:
                                    {
                                        //twoRow_6_2
                                        [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"shadowImageView"] setImage:[UIImage imageNamed:@"threeRow_11_2.png"]];
                                    }
                                        break;
                                    case 1:
                                    case 2:
                                    case 3:
                                    {
                                        [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"shadowImageView"] setImage:[UIImage imageNamed:@"firstRow_234_2.png"]];
                                    }
                                        break;
                                    case 4:
                                    {
                                        [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"shadowImageView"] setImage:[UIImage imageNamed:@"firstRow_5_2.png"]];
                                    }
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }
                        else
                        {
                            switch ((idx)%5) {
                                case 0:
                                {
                                    //threeRow_11_2
                                    [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"shadowImageView"] setImage:[UIImage imageNamed:@"twoRow_10_2.png"]];
                                }
                                    break;
                                case 1:
                                case 2:
                                case 3:
                                {
                                    [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"shadowImageView"] setImage:[UIImage imageNamed:@"twoRow_987_2.png"]];
                                }
                                    break;
                                case 4:
                                {
                                    [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"shadowImageView"] setImage:[UIImage imageNamed:@"twoRow_6_2.png"]];
                                }
                                    break;
                                default:
                                    break;
                            }
                        }
                        *stop = YES;
                    }
                }];
                [wself addSubview:allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"shadowImageView"]];
                /*************************************************************************/
            }
            }
            
        } completion:^(BOOL finished) {
            /***********************节点ID和按钮ID相对应************************/
                //根据节点ID获取此节点中，屏幕的信息:
                if([allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"isNodePagePlay"] intValue] == 1)
                {
                    TopLargerScreenView *topLargerScr = allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"topLargerScr"];
                    [topLargerScr setContentOffset:CGPointMake(BUTTONID*(Screen_Width-20), 0) animated:YES];
                    AllNodesInScrollView *allNodesView = allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"bottomSmaPic"];
                    [allNodesView.delagateNodeForTapOrPlay HandleTapNodeButton:self ButtonIndex:@(BUTTONID)];
                }
                else
                {
                    TopLargerScreenView *topLargerScr = allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"topLargerScr"];
                    [topLargerScr setContentOffset:CGPointMake(BUTTONID*(Screen_Width-20), 0) animated:YES];
                    AllNodesInScrollView *allNodesView = allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"bottomSmaPic"];
                    [allNodesView.delagateFlowForTapOrPlay HandleTapButtonForPlay:allNodesView ButtonIndex:@(BUTTONID)];
                }
                /****************************************************************/
                //赋值滑动列表选择频道ID
                scrollViewSelectedChannelID = sender.tag;
//            }
        }];
        
    }
    //重复点击选中按钮
    else {
        
    }
    
    
}
//长按按钮 切屏:
- (void)longPanGesture:(UILongPressGestureRecognizer *)longPanGesture
{
    NSLog(@"longPan");
    
    if (isHaveFlowPlaying == YES && currentFlowIsPlaying == NO) {
        [[MBProgressController sharedInstance] showTipsOnlyText:@"其他流程正在播放" AndDelay:1.5];
        return;
    }
    [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"nodeButtonArr"] enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL *stop){
        NSDictionary *dic = (NSDictionary *)obj;
        UIButton *button = [dic objectForKey:@(idx)];
        button.titleLabel.textColor = [UIColor whiteColor];
        for (UIGestureRecognizer *v in button.gestureRecognizers) {
            if ([v isKindOfClass:[UIPanGestureRecognizer class]]) {
                [button removeGestureRecognizer:v];
            }
        }
        [button.layer removeAllAnimations];
    }];
    
    [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"nodeButtonArr"] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSDictionary *dic = (NSDictionary *)obj;
        UIButton *button = [dic objectForKey:@(idx)];
        
        if(button.frame.origin.x == longPanGesture.view.frame.origin.x && button.frame.origin.y == longPanGesture.view.frame.origin.y)
        {
            /****************************节点抖动效果*/
            [button setAlpha:0.7];
            [button.layer setShadowColor:[UIColor grayColor].CGColor];
            [button.layer setShadowOpacity:1.0f];
            [button.layer setShadowRadius:10.0f];
            self.shakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform"];
            self.shakeAnimation.duration = 0.08;
            self.shakeAnimation.autoreverses = YES;
            self.shakeAnimation.repeatCount = MAXFLOAT;
            self.shakeAnimation.fromValue = [NSValue valueWithCATransform3D:CATransform3DRotate(button.layer.transform, -0.1, 0, 0, 1)];
            self.shakeAnimation.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(button.layer.transform, 0.1, 0, 0, 1)];
            [button.layer addAnimation:self.shakeAnimation forKey:@"shakeAnimation"];
            /*****************************************************/
            *stop = YES;
        }
    }];
    
    if([allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"playFlag"] intValue] == 1)
    {
        AllNodesInScrollView *allNodesView = allFlowDataArr[isPlayingFlowNumber][isPlayingFlowId][@"bottomSmaPic"];
        if (timer_playFlowScreen != nil) {
            dispatch_source_cancel(timer_playFlowScreen);
        }
        allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"playFlag"] = @(0);
        allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"pauseFlag"] = @(1);
        //可以发一个通知，让按钮再发一个函数
        [allNodesView.delagateNodeForTapOrPlay HandlePausePlay:self ButtonIndex:@(onceOrcycles)];
    }
    
    CGPoint point = [longPanGesture locationInView:self];
    
    if (longPanGesture.state == UIGestureRecognizerStateBegan)
    {
        CGRect f = longPanGesture.view.frame;
        f.origin.y -= self.dHeight;
        longPanGesture.view.frame = f;
        
        lastPoint = point;
        _startPoint = longPanGesture.view.center;

        self.panStartPosition = longPanGesture.view.center.y;
        
        self.pannedView = longPanGesture.view;
        
        [self.superview addSubview:self.pannedView];//把这个view 提取出来
    }
    else if (longPanGesture.state == UIGestureRecognizerStateChanged)
    {
        [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"nodeButtonArr"] enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL *stop){
            NSDictionary *dic = (NSDictionary *)obj;
            UIButton *button = [dic objectForKey:@(idx)];
            if(button.frame.origin.x == longPanGesture.view.frame.origin.x && button.frame.origin.y == longPanGesture.view.frame.origin.y)
            {
                [button.layer removeAllAnimations];
            }
        }];
        
        float offX = point.x - lastPoint.x;
        float offY = point.y - lastPoint.y;
        [self.pannedView setCenter:CGPointMake(self.pannedView.center.x + offX, self.pannedView.center.y + offY)];
        self.panEndPosition = point.y;
        
        NSLog(@"%f",self.panStartPosition);
        NSLog(@"%f",self.panEndPosition);
        self.projectViewRect = self.pannedView.frame;
        
        lastPoint = point;
    }
    else if (longPanGesture.state == UIGestureRecognizerStateEnded)
    {
        [UIView animateWithDuration:0.2
                              delay:0
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             
                             CGRect f = longPanGesture.view.frame;
                             f.origin.y += self.dHeight;
                             longPanGesture.view.frame = f;
                             
                             _startPoint.y += self.dHeight;
                             
                             self.panEndPosition -= self.dHeight;
                             
                             longPanGesture.view.center = _startPoint;
                             [self.pannedView removeFromSuperview];
                             self.pannedView = nil;
                             [self addSubview:longPanGesture.view];
                         }
                         completion:^(BOOL finished) {
                             
                             [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"nodeButtonArr"] enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL *stop){
                                 NSDictionary *dic = (NSDictionary *)obj;
                                 UIButton *button = [dic objectForKey:@(idx)];
                                 [button.layer removeAllAnimations];
                             }];
                             if(self.panEndPosition + 90< 0)
                             {
                                
                                 [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"nodeButtonArr"] enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL *stop){
                                     NSDictionary *dic = (NSDictionary *)obj;
                                     UIButton *button = [dic objectForKey:@(idx)];
                                     button.selected = NO;
                                 }];
                                 self.panEndPosition = 0;
                                 isProjectScreen = YES;
                                 
                                 
                                 [self.delagateNodeForTapOrPlay HandleNoteButtonLongPressPlay:self ButtonID:scrollViewSelectedChannelID-100 RECT:self.projectViewRect];
                                 
                                 
                                 [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"shadowImageView"] removeFromSuperview];
                                 allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"shadowImageView"] = [[UIImageView alloc] initWithFrame:CGRectMake(longPanGesture.view.frame.origin.x, longPanGesture.view.frame.origin.y,longPanGesture.view.frame.size.width, longPanGesture.view.frame.size.height)];
                                 
                                 
                                 /************************************************************************/
                                 [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"nodeButtonArr"] enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL *stop){
                                     NSDictionary *dic = (NSDictionary *)obj;
                                     UIButton *button = [dic objectForKey:@(idx)];
                                     if(button.frame.origin.x == longPanGesture.view.frame.origin.x && button.frame.origin.y == longPanGesture.view.frame.origin.y)
                                     {
                                         allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"havePlayNodeIndex"] = @(idx);
                                         
                                         button.selected = YES;
                                         userSelectedChannelID = button.tag;
                                         
                                         TopLargerScreenView    *topLargerScr = allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"topLargerScr"];
                                         [topLargerScr setContentOffset:CGPointMake((button.tag-100)*(Screen_Width-20), 0) animated:YES];
                                         allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"havePlayNodeIndex"] = @(button.tag-100);
                                         /***********************节点ID和按钮ID相对应************************/
                                         //根据节点ID获取此节点中，屏幕的信息:
                                         if([allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"isNodePagePlay"] intValue] == 1)
                                         {
                                             [self.delagateNodeForTapOrPlay HandleTapNodeButton:self ButtonIndex:@(button.tag-100)];
                                         }
                                         else
                                         {
                                             [self.delagateFlowForTapOrPlay HandleTapButtonForPlay:self ButtonIndex:@(button.tag-100)];
                                         }
                                         /****************************************************************/
                                         
                                         if((idx/5)%2 == 0)
                                         {
                                             if(idx==0)
                                             {
                                                 [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"shadowImageView"] setImage:[UIImage imageNamed:@"firstRow_1_2.png"]];
                                             }
                                             else
                                             {
                                                 switch (idx%5) {
                                                     case 0:
                                                     {
                                                         //twoRow_6_2.png
                                                         [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"shadowImageView"] setImage:[UIImage imageNamed:@"threeRow_11_2.png"]];
                                                     }
                                                         break;
                                                     case 1:
                                                     case 2:
                                                     case 3:
                                                     {
                                                         [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"shadowImageView"] setImage:[UIImage imageNamed:@"firstRow_234_2.png"]];
                                                     }
                                                         break;
                                                     case 4:
                                                     {
                                                         [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"shadowImageView"] setImage:[UIImage imageNamed:@"firstRow_5_2.png"]];
                                                     }
                                                         break;
                                                     default:
                                                         break;
                                                 }
                                             }
                                         }
                                         else
                                         {
                                             switch (idx%5) {
                                                 case 0:
                                                 {
                                                     //threeRow_11_2.png
                                                     [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"shadowImageView"] setImage:[UIImage imageNamed:@"twoRow_10_2.png"]];
                                                 }
                                                     break;
                                                 case 1:
                                                 case 2:
                                                 case 3:
                                                 {
                                                     [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"shadowImageView"] setImage:[UIImage imageNamed:@"twoRow_987_2.png"]];
                                                 }
                                                     break;
                                                 case 4:
                                                 {
                                                     [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"shadowImageView"] setImage:[UIImage imageNamed:@"twoRow_6_2.png"]];
                                                 }
                                                     break;
                                                 default:
                                                     break;
                                             }
                                         }
                                         *stop = YES;
                                     }
                                 }];
                                 /*************************************************************************/
                                 [self addSubview:allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"shadowImageView"]];
                             }
                         }];
    }
}


- (void)adjustScrollViewContentX:(UIButton *)sender
{
//    float originX = [[[_buttonOriginXArray objectAtIndex:BUTTONID] objectForKey:@"xPos"] floatValue];
//    float originY = [[[_buttonOriginXArray objectAtIndex:BUTTONID] objectForKey:@"yPos"] floatValue];
    //float width = [[_buttonWithArray objectAtIndex:BUTTONID] floatValue];
//    
//    if (sender.frame.origin.x - self.contentOffset.x > CONTENTSIZEX-(BUTTONGAP+width)) {
//        [self setContentOffset:CGPointMake(originX - 30, 0)  animated:YES];
//    }
    NSLog(@"%ld",BUTTONID);
    //[self setContentOffset:CGPointMake(originX, originY)  animated:YES];
    
//    if (sender.frame.origin.x - self.contentOffset.x < 5) {
//        [self setContentOffset:CGPointMake(originX,0)  animated:YES];
//    }
}

//滚动内容页顶部滚动
- (void)setButtonUnSelect
{
    //滑动撤销选中按钮
    UIButton *lastButton = (UIButton *)[self viewWithTag:scrollViewSelectedChannelID];
    lastButton.selected = NO;
    
//    [shadowImageView removeFromSuperview];
//    shadowImageView = nil;
    [allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"nodeButtonArr"] enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOL *stop){
        NSDictionary *dic = (NSDictionary *)obj;
        UIButton *button = [dic objectForKey:@(idx)];
        button.selected = NO;
        [button addTarget:self action:@selector(selectNameButton:) forControlEvents:UIControlEventTouchUpInside];
    }];
}

- (void)setButtonSelect
{
    //滑动选中按钮
    UIButton *button = (UIButton *)[self viewWithTag:scrollViewSelectedChannelID];
    
    [UIView animateWithDuration:0.25 animations:^{
        
//        float originX = [[[_buttonOriginXArray objectAtIndex:button.tag-100] objectForKey:@"xPos"] floatValue];
//        float originY = [[[_buttonOriginXArray objectAtIndex:button.tag-100] objectForKey:@"yPos"] floatValue];
//        [shadowImageView setFrame:CGRectMake(button.frame.origin.x, 10, [[_buttonWithArray objectAtIndex:button.tag-100] floatValue], 92)];
//        if(isHavePlayInFlow == YES)
//        {
//            [shadowImageView setFrame:CGRectMake(originX, originY, [[_buttonWithArray objectAtIndex:button.tag-100] floatValue], 92)];
//        }
        
    } completion:^(BOOL finished) {
        if (finished) {
            if (!button.selected) {
                button.selected = YES;
                userSelectedChannelID = button.tag;
                allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"haveChooseNodeIndex"] = @(button.tag - 100);
            }
        }
    }];
}

-(void)setScrollViewContentOffset
{
//    float originX = [[[_buttonOriginXArray objectAtIndex:BUTTONSELECTEDID] objectForKey:@"xPos"] floatValue];
    float originY = [[[_buttonOriginXArray objectAtIndex:BUTTONSELECTEDID] objectForKey:@"yPos"] floatValue];
//    float width = [[_buttonWithArray objectAtIndex:BUTTONSELECTEDID] floatValue];
    
    [self setContentOffset:CGPointMake(0, originY-10) animated:YES];
    
//    if (originX - self.contentOffset.x > CONTENTSIZEX-(BUTTONGAP+width)) {
//        [self setContentOffset:CGPointMake(originX - 30, 0)  animated:YES];
//    }
    NSLog(@"%ld",BUTTONSELECTEDID);
    //[self setContentOffset:CGPointMake(originX, originY)  animated:YES];
//    if (originX - self.contentOffset.x < 5) {
//        [self setContentOffset:CGPointMake(originX,0)  animated:YES];
//    }
}

#pragma mark-
#pragma mark- UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    NSLog(@"%f,%f,%f,%f",scrollView.frame.origin.x,scrollView.frame.origin.y,scrollView.frame.size.width,scrollView.frame.size.height);
    self.dHeight = scrollView.contentOffset.y;
    NSLog(@"=======>%lf",self.dHeight);
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"%f,%f,%f,%f",scrollView.frame.origin.x,scrollView.frame.origin.y,scrollView.frame.size.width,scrollView.frame.size.height);
}
@end
